//
//  LwxWorldAddressPcikerView.m
//  test
//
//  Created by 鲁万玺 on 2017/7/28.
//  Copyright © 2017年 鲁万玺. All rights reserved.
//

#import "LwxWorldAddressPcikerView.h"

@interface LwxWorldAddressPcikerView () <UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *pickView;

@property (strong, nonatomic) NSDictionary *locationDic;
@property (nonatomic ,strong)NSArray *continentsArray; /**<洲*/
@property (nonatomic ,strong)NSArray *countryRegionArray;/**<国家*/
@property (nonatomic ,strong)NSArray *stateArray;/**<省*/
@property (nonatomic ,strong)NSArray *cityStateArray;/**<市*/

@end

@implementation LwxWorldAddressPcikerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self getAddressInformation];
        [self setBaseView];
    }
    return self;
}

- (void)getAddressInformation
{
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Loaction" ofType:@"plist"];
    self.locationDic = [[NSDictionary alloc] initWithContentsOfFile:path1][@"Location"];
    
    self.continentsArray = self.locationDic[@"Continent"];
    self.countryRegionArray = self.continentsArray[0][@"CountryRegion"];
    self.stateArray = self.countryRegionArray[0][@"State"];
    self.cityStateArray = self.stateArray[0][@"City"];
}

- (void)setBaseView
{
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    UIColor *color = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:249/255.0 alpha:1];
    UIColor *btnColor = [UIColor colorWithRed:65.0/255 green:164.0/255 blue:249.0/255 alpha:1];
    UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0, height - 210, width, 30)];
    selectView.backgroundColor = color;
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setTitle:@"取消" forState:0];
    [cancleBtn setTitleColor:btnColor forState:0];
    cancleBtn.frame = CGRectMake(0, 0, 60, 40);
    [cancleBtn addTarget:self action:@selector(dateCancleAction) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:cancleBtn];
    
    UIButton *ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ensureBtn setTitle:@"确定" forState:0];
    [ensureBtn setTitleColor:btnColor forState:0];
    ensureBtn.frame = CGRectMake(width - 60, 0, 60, 40);
    [ensureBtn addTarget:self action:@selector(dateEnsureAction) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:ensureBtn];
    [self addSubview:selectView];
    
    self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, height - 180 , width,  180)];
    self.pickView.delegate   = self;
    self.pickView.dataSource = self;
    self.pickView.backgroundColor = color;
    [self addSubview:self.pickView];
    [self.pickView reloadAllComponents];
    
    [self updateAddress];
    
}

- (void)dateCancleAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(LwxWorldAddressCancleAction:)])
    {
        [self.delegate LwxWorldAddressCancleAction:@""];
        [self removeFromSuperview];
    }
}

- (void)dateEnsureAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(LwxWorldAddressPickerRerurnBlockWithContinents:countryRegion:state:cityState:)])
    {
        [self.delegate LwxWorldAddressPickerRerurnBlockWithContinents:self.continents countryRegion:self.countryRegion state:self.state cityState:self.cityState];
        [self removeFromSuperview];
    }
}

- (void)showPicker
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel)
    {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:self.font?:[UIFont boldSystemFontOfSize:14]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 4;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:return self.continentsArray.count;
            
        case 1:return self.countryRegionArray.count;
            
        case 2:return self.stateArray.count;
            
        default:return self.cityStateArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    switch (component)
    {
        case 0:return self.continentsArray[row][@"Name"];
            
        case 1:return self.countryRegionArray[row][@"Name"];
            
        case 2:return self.stateArray[row][@"Name"];
            
        default:return self.cityStateArray[row][@"Name"];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return self.frame.size.width / 4;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        if ([self.continentsArray[row][@"CountryRegion"] isKindOfClass:[NSArray class]])
        {
            self.countryRegionArray = self.continentsArray[row][@"CountryRegion"];
            
            if ([self.countryRegionArray[0][@"State"] isKindOfClass:[NSArray class]])
            {
                self.stateArray = self.countryRegionArray[0][@"State"];
                self.cityStateArray = self.stateArray[0][@"City"];
            }
            else
            {
                self.stateArray = self.countryRegionArray[0][@"State"][@"City"];
                self.cityStateArray = @[];
            }
        }
        else
        {
            self.countryRegionArray = @[@{@"Name":self.continentsArray[row][@"CountryRegion"][@"Name"]}];
            self.stateArray = @[];
            self.cityStateArray = @[];
        }
        
        [pickerView reloadComponent:1];
        [pickerView selectedRowInComponent:1];
        [pickerView reloadComponent:2];
        [pickerView selectedRowInComponent:2];
        [pickerView reloadComponent:3];
        [pickerView selectedRowInComponent:3];
    }
    if (component == 1)
    {
        if ([self.countryRegionArray[row][@"State"] isKindOfClass:[NSArray class]])
        {
            self.stateArray = self.countryRegionArray[row][@"State"];
            self.cityStateArray = self.stateArray[0][@"City"];
        }
        else
        {
            self.stateArray = self.countryRegionArray[row][@"State"][@"City"];
            self.cityStateArray = @[];
        }
        
        [pickerView reloadComponent:2];
        [pickerView selectedRowInComponent:2];
        [pickerView reloadComponent:3];
        [pickerView selectedRowInComponent:3];
    }
    if (component == 2)
    {
        self.cityStateArray = self.stateArray[row][@"City"];
        [pickerView reloadComponent:3];
        [pickerView selectedRowInComponent:3];
    }
    [self updateAddress];
}

- (void)updateAddress
{
    self.continents = [self.continentsArray objectAtIndex:[self.pickView selectedRowInComponent:0]][@"Name"];
    if (self.countryRegionArray.count == 0)
    {
        self.countryRegion = @"";
        self.state = @"";
        self.cityState = @"";
    }
    else
    {
        self.countryRegion = [self.countryRegionArray objectAtIndex:[self.pickView selectedRowInComponent:1]][@"Name"];
        if (self.stateArray.count == 0)
        {
            self.state = @"";
            self.cityState = @"";
        }
        else
        {
            self.state = [self.stateArray objectAtIndex:[self.pickView selectedRowInComponent:2]][@"Name"];
            if (self.cityStateArray.count == 0)
            {
                self.cityState = @"";
            }
            else
            {
                self.cityState = [self.cityStateArray objectAtIndex:[self.pickView selectedRowInComponent:3]][@"Name"];
            }
        }
    }
    
}

@end

